﻿using System.Web.Mvc;
using System.Web.Security;

namespace Nasa8x.Mvc.Attributes
{
    public class RequiredAuthenticationAttribute : AuthorizeAttribute
    {

        //public string ReturnUrl { get; set; }

        public override void OnAuthorization(AuthorizationContext _filterContext)
        {
            //redirect if not authenticated
            if (!_filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //use the current url for the redirect
                if (_filterContext.HttpContext.Request.Url != null)
                {
                    string _redirectOnSuccess = _filterContext.HttpContext.Request.Url.AbsolutePath;
                    //send them off to the login page
                    string _redirectUrl = string.Format("?ReturnUrl={0}", _redirectOnSuccess);
                    string _loginUrl = FormsAuthentication.LoginUrl + _redirectUrl;
                    _filterContext.HttpContext.Response.Redirect(_loginUrl, true);
                }
            }

        }
    }
}